package no.spk.demo.tcalc;


import java.awt.List;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import no.spk.demo.tcalc.amclient.AMClient;
import no.spk.demo.tcalc.amclient.Am;
import no.spk.demo.tcalc.domain.CClass;

import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
import org.jboss.resteasy.client.ProxyFactory;
import org.jboss.resteasy.plugins.providers.RegisterBuiltin;
import org.jboss.resteasy.plugins.providers.jaxb.JAXBXmlRootElementProvider;
import org.jboss.resteasy.spi.ResteasyProviderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args ) 
    {
        System.out.println( "Hello World!" );
        
        RegisterBuiltin.register(ResteasyProviderFactory.getInstance());
        ResteasyProviderFactory.getInstance().addMessageBodyReader(JAXBXmlRootElementProvider.class);
        
        
       Am am = new Am();
//       String xmlResult = amClient.getChar("Azuremyst", "Piety" );
       
       String guildXmlResult = am.getGuild( "Azuremyst", "Crit for Brains" );
       System.out.println( guildXmlResult );
       

        try {
            DocumentBuilder builder = createDocumentBuilder();
            Document guildDoc = builder.parse(new InputSource(new StringReader(guildXmlResult)));

            XPathFactory factory = XPathFactory.newInstance();
            XPath xpath = factory.newXPath();

            javax.xml.xpath.XPathExpression charactersXpath = xpath.compile("//guildInfo/guild/members/character");
            javax.xml.xpath.XPathExpression nameXpath = xpath.compile("@name");
            javax.xml.xpath.XPathExpression levelXpath = xpath.compile("@level");
            javax.xml.xpath.XPathExpression classIdXpath = xpath.compile("@classId");

            NodeList charactersNodeList = (NodeList) charactersXpath.evaluate(guildDoc, XPathConstants.NODESET);
            
            ArrayList<String> chars = new ArrayList<String>();
            ArrayList<Double> tankIds = new ArrayList<Double>();
            tankIds.add( Double.valueOf(1) ); // Warrior
            tankIds.add( Double.valueOf(2) ); // Paladin
            tankIds.add( Double.valueOf(6) ); // Death Knight
            tankIds.add( Double.valueOf(11) ); // Druid
            
            for (int i = 0; i < charactersNodeList.getLength(); i++) {
                Node charNode = charactersNodeList.item(i);
                Double level = (Double) levelXpath.evaluate(charactersNodeList.item(i), XPathConstants.NUMBER);
                Double classId = (Double) classIdXpath.evaluate(charactersNodeList.item(i), XPathConstants.NUMBER);
                String name = (String) nameXpath.evaluate(charactersNodeList.item(i), XPathConstants.STRING);
                System.out.println( name + ":" + level);
                
                if( (level == 80) && tankIds.contains( classId ) )
                    chars.add( name );                
            }
            
            System.out.println( chars.size() + " chars \n" + chars );
            System.out.printf( "%1$-13s %2$-6s %3$-9s %4$-6s %5$-6s %6$-6s\n" , "Name", "Mit", "EH", "Miss", "Avoid", "DPRatio" );
            
            
            for( String member : chars ){                
//                System.out.println( "------------------------------------\n- " + member );
                String memberXmlResult = am.getChar("Azuremyst", member );
                ArrayList<String> specsXmlResult = new ArrayList<String>();
                
                // Get specs
                for(int i = 1; i<3; i++){
                    specsXmlResult.add( am.getTalents("Azuremyst", member, i+"") );                    
                }                
                

                
                Document charDoc = builder.parse(new InputSource(new StringReader(memberXmlResult)));
                
                // Get items
                javax.xml.xpath.XPathExpression itemIdXpath = xpath.compile("//characterInfo/characterTab/items/item/@id");

                NodeList itemIdNodeList = (NodeList) itemIdXpath.evaluate(charDoc, XPathConstants.NODESET);
                ArrayList<String> itemsXmlResult = new ArrayList<String>();                
                
                for(int i = 0; i < itemIdNodeList.getLength(); i++) {
                    Node itemNode = itemIdNodeList.item(i);
                    itemsXmlResult.add( am.getItem( itemNode.getNodeValue() ) );                    
                }  
                
                
                
                XPathExpression specsXpath = xpath.compile("//characterInfo/characterTab/talentSpecs/talentSpec");
                XPathExpression primSpecXpath = xpath.compile("@prim");
                
                NodeList specsNodeList = (NodeList) specsXpath.evaluate(charDoc, XPathConstants.NODESET);                
                
                for (int i = 0; i < specsNodeList.getLength(); i++) {
                    Node specNode = specsNodeList.item(i);
//                    System.out.println( (String) primSpecXpath.evaluate(specNode, XPathConstants.STRING) );
                }
                
                XPathExpression armorXpath = xpath.compile("//characterInfo/characterTab/defenses/armor/@effective");
                XPathExpression plusDefenseXpath = xpath.compile("//characterInfo/characterTab/defenses/defense/@plusDefense");
                XPathExpression dodgeXpath = xpath.compile("//characterInfo/characterTab/defenses/dodge/@percent");
                XPathExpression parryXpath = xpath.compile("//characterInfo/characterTab/defenses/parry/@percent");
                
                XPathExpression healthXpath = xpath.compile("//characterInfo/characterTab/characterBars/health/@effective");
                
                CClass cClass = new CClass();
                cClass.setName(member);
                
                cClass.setArm( ((Double) armorXpath.evaluate(charDoc, XPathConstants.NUMBER)).intValue() );
                cClass.setPlusDef( ((Double) plusDefenseXpath.evaluate(charDoc, XPathConstants.NUMBER)).intValue() );
                cClass.setDod( ((Double) dodgeXpath.evaluate(charDoc, XPathConstants.NUMBER)).doubleValue() );
                cClass.setPar( ((Double) parryXpath.evaluate(charDoc, XPathConstants.NUMBER)).doubleValue() );
                
                cClass.setHp( ((Double) healthXpath.evaluate(charDoc, XPathConstants.NUMBER)).intValue() );
                
                
                System.out.printf( "%1$-13s %2$6.2f %3$9.2f %4$6.2f %5$6.2f %6$6.2f\n" , cClass.getName(), cClass.getMit(), cClass.getEH(), cClass.getMiss(), cClass.getAvoid(), cClass.getDPRatio() );
            }

        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (XPathExpressionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
              
    }
    

    
    private static DocumentBuilder createDocumentBuilder() throws ParserConfigurationException {
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(true); // never forget this!
        DocumentBuilder builder = domFactory.newDocumentBuilder();
        return builder;
    }
}
